﻿/**
 * 
 * Copyright 2009 Mike Jamer [email: mikejamer@gmail.com ]
 * 
 * This File is part of Moose IRC
 * 
 * MooseIRC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MooseIRC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MooseIRC.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace moose.irc
{
    /// <summary>
    /// Represents an Irc Channel
    /// </summary>
    [CLSCompliant(true)]
    public class IrcChannel : object
    {
        /// <summary>
        /// Channel Modes
        /// </summary>
        private Dictionary<string, IrcChannelMode> _dictModes;

        /// <summary>
        /// Users in Channel
        /// </summary>
        private Dictionary<string, IrcUser> _dictUsers;

        /// <summary>
        /// Channel Name
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Channel Topic
        /// </summary>
        public string Topic { get; set; }

        /// <summary>
        /// Event indicates a private message
        /// </summary>
        public event ResponseHandler ChannelMessage;

        /// <summary>
        /// Enumerates Current Channel Modes
        /// </summary>
        public IEnumerable<IrcChannelMode> Modes
        {
            get
            {
                return _dictModes.Values;
            }
        }

        public Dictionary<string, IrcUser> Users {
            get {
                return _dictUsers;
            }
            set {
                _dictUsers = value;
                return;
            }
        }

        /// <summary>
        /// Initialize Irc Channel
        /// </summary>
        public IrcChannel()
            : base()
        {
            _dictUsers = new Dictionary<string, IrcUser>();
            _dictModes = new Dictionary<string, IrcChannelMode>();
            ChannelMessage += onChannelMessage;
            return;
        }

        public void AddUser(string user)
        {
            _dictUsers[user] = new IrcUser() { Nickname = user }; 
            return;
        }

        public void RemoveUser(string user) 
        {
            _dictUsers.Remove(user);
            return;
        }

        public void RenameUser(string user_original, string user_new) {
            _dictUsers[user_original].Nickname = user_new;
            _dictUsers[user_new] = _dictUsers[user_original];
            _dictUsers.Remove(user_original);
        }

        /// <summary>
        /// Add a channel mode
        /// </summary>
        /// <param name="mode">new channel mode</param>
        /// <param name="parm">mode parameters</param>
        public void AddMode(string mode, string parm)
        {
            _dictModes.Add(mode, new IrcChannelMode() { Mode=mode, Parameter = parm });
            return;
        }

        /// <summary>
        /// Remove a channel mode
        /// </summary>
        /// <param name="mode">mode to remove</param>
        public void RemoveMode(string mode)
        {
            _dictModes.Remove(mode);
            return;
        }

        /// <summary>
        /// Indicates a messaage for channel
        /// </summary>
        /// <param name="message">message sent to channel</param>
        public void echo(string message) {
            ChannelMessage(message);
            return;
        }

        /// <summary>
        /// Internal Handler for private message
        /// </summary>
        /// <param name="message">message</param>
        private void onChannelMessage(string message) {
            return;
        }

    }
}
